<?php
/**
 * 静态资源文件管理
 */

namespace backend\controllers;


use backend\utils\XResources;
use yii;

class ResourcesController extends BaseController
{
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionCssIndex()
    {
        return $this->render('css-index');
    }

    public function actionCssSystem()
    {
        if (Yii::$app->request->post()) {
            $data = Yii::$app->request->post('contents');
            XResources::saveCssSystem($data);
        }
        $contents = XResources::getCssSystem();
        return $this->render('css-system', ['contents' => $contents]);
    }

    public function actionCss()
    {
        $files = XResources::getCssList();
        return $this->render('css', ['files' => $files]);
    }

    public function actionCssCreate()
    {
        if (Yii::$app->request->post()) {
            $name = Yii::$app->request->post('name');
            if (!$name) {
                $this->tips("文件名不能为空！");
            }
            $data = Yii::$app->request->post('contents');
            if (XResources::saveCss($name . '.css', $data)) {
                $this->redirect(['css'], 301);
            }
        }
        return $this->render('css-form', ['name' => '', 'contents' => '']);
    }

    public function actionCssUpdate($id)
    {
        $contents = XResources::getCssContent($id);
        if (Yii::$app->request->post()) {
            $data = Yii::$app->request->post('contents');
            if (XResources::saveCss($id, $data)) {
                $this->redirect(['css'], 301);
            }
        }
        return $this->render('css-form', ['name' => $id, 'contents' => $contents]);
    }

    public function actionCssDelete($id)
    {
        if (XResources::deleteCss($id)) {
            $this->redirect(['css'], 301);
        } else {
            $this->tips("删除失败！");
        }
    }

    public function actionImagesIndex()
    {
        return $this->render('images-index');
    }

    public function actionImagesSystem()
    {
        $files = XResources::getImagesSystemList();
        return $this->render('images-system', ['files' => $files]);
    }

    public function actionImagesSystemDelete()
    {
        $path = Yii::$app->request->post('path');
        $files = XResources::deleteImagesSystem($path);
        echo $files;
    }

    public function actionImagesSystemCreate()
    {
        return $this->render('images-system-create');
    }

    public function actionImages()
    {
        $files = XResources::getImagesList();
        return $this->render('images', ['files' => $files]);
    }

    public function actionImagesCreate()
    {
        return $this->render('images-create');
    }

    public function actionJs()
    {
        $files = XResources::getJsList();
        return $this->render('js', ['files' => $files]);
    }

    public function actionJsCreate()
    {
        if (Yii::$app->request->post()) {
            $name = Yii::$app->request->post('name');
            if (!$name) {
                $this->tips("文件名不能为空！");
            }
            $data = Yii::$app->request->post('contents');
            if (XResources::saveJs($name . '.js', $data)) {
                $this->redirect(['js'], 301);
            }
        }
        return $this->render('js-form', ['name' => '', 'contents' => '']);
    }

    public function actionJsUpdate($id)
    {
        $contents = XResources::getJsContent($id);
        if (Yii::$app->request->post()) {
            $data = Yii::$app->request->post('contents');
            if (XResources::saveJs($id, $data)) {
                $this->redirect(['js'], 301);
            }
        }
        return $this->render('js-form', ['name' => $id, 'contents' => $contents]);
    }

    public function actionJsDelete($id)
    {
        if (XResources::deleteJs($id)) {
            $this->redirect(['js'], 301);
        } else {
            $this->tips("删除失败！");
        }
    }

    /**
     * 通过\kartik\file\FileInput上传组图
     * @return array
     */
    public function actionUploadImages()
    {
        Yii::$app->response->format = yii\web\Response::FORMAT_JSON;
        $type = Yii::$app->request->get('type');
        $file = yii\web\UploadedFile::getInstanceByName('files');
        if (!$file) {
            return false;
        }
        if ($type == 'sys') {
            $path = Yii::getAlias('@frontend') . '/static/images/' . $file->name;
        } else {
            $path = Yii::getAlias('@frontend') . '/web/images/' . $file->name;
        }
        return $file->saveAs($path);
    }
}